<template>
  <div class="home-container">
    <header class="header">
      <div class="logo-home">大麦电影票务平台</div>
      <div class="nav-menu">
        <el-menu
            :default-active="activeMenu"
            mode="horizontal"
            background-color="#e54847"
            text-color="#fff"
            active-text-color="#ffd04b"
        >
          <el-menu-item index="1">首页</el-menu-item>
          <el-menu-item index="2">电影</el-menu-item>
          <el-menu-item index="3">演出</el-menu-item>
          <el-menu-item index="4">体育</el-menu-item>
          <el-menu-item index="5">我的订单</el-menu-item>
        </el-menu>
      </div>
      <div class="user-info">
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link user-dropdown">
            <el-avatar
                :size="40"
                :src="userAvatar"
                class="user-avatar"
            />
            <span class="username">{{ userName }}</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人中心</el-dropdown-item>
              <el-dropdown-item command="orders">我的订单</el-dropdown-item>
              <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <main class="main-content">
      <div class="layout-grid">
        <div class="panel">
          <ChatBox />
        </div>
        <div class="panel carousel-container">
          <Carousel />
        </div>
        <div class="panel ranking-container">
          <Ranking />
        </div>
      </div>

      <div class="ticket-list-container">
        <TicketList />
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import orderService from '@/services/order'
import ChatBox from './home/ChatBox.vue'
import Carousel from './home/Carousel.vue'
import Ranking from './home/Ranking.vue'
import TicketList from './home/TicketList.vue'

const authStore = useAuthStore()
const router = useRouter()
const activeMenu = ref('1')

// 用户数据状态
const userData = ref(null)
const userAvatar = ref('https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png') // 默认头像
const userName = ref('用户')
const isLoading = ref(false) // 加载状态
const errorMessage = ref('') // 错误信息

// 后端API地址
const API_URL = '/api/user/profile'

// 页面加载时获取用户数据
onMounted(() => {
  fetchUserData()
})

// 获取用户数据
const fetchUserData = async () => {
  isLoading.value = true
  errorMessage.value = ''

  try {
    // 使用封装的request工具，自动携带token
    const response = await request.get(API_URL)

    // ✅ 修改：根据request.js的封装，直接使用response.code
    if (response.code === 200) {
      // ✅ 修改：根据后端实际返回结构调整
      const data = response.data
      userData.value = data
      userAvatar.value = data.avatarImg || userAvatar.value
      userName.value = data.username || '用户'

      // 可选：更新store中的用户信息
      if (data) {
        authStore.setUser(data)
      }
    } else {
      // 业务错误
      errorMessage.value = response.message || '获取用户数据失败'
      ElMessage.error(errorMessage.value)
    }
  } catch (error) {
    // 网络错误
    console.error('获取用户数据出错', error)

    // 获取详细错误信息
    const errorInfo = error.response?.data || error.message || '未知错误'
    errorMessage.value = `网络错误: ${JSON.stringify(errorInfo)}`
    ElMessage.error(errorMessage.value)
  } finally {
    isLoading.value = false
  }
}

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'orders') {
    router.push('/orders') // 添加订单页面跳转
  } else if (command === 'logout') {
    authStore.clearAuth()
    router.push('/login')
    ElMessage.success('已退出登录')
  }
}
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  align-items: center;
  background-color: #e54847;
  color: white;
  padding: 0 20px;
  height: 60px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.logo-home {
  font-size: 24px;
  font-weight: bold;
  margin-right: 40px;
}

.nav-menu {
  flex: 1;
}

.user-info {
  display: flex;
  align-items: center;
}

.username {
  margin-left: 10px;
  color: white;
  font-size: 16px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-avatar {
  border: 1px solid #fff;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}
/* 其他样式保持不变 */
.main-content {
  padding: 20px;
  background-color: #f5f7fa;
  flex: 1;
}

.layout-grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: 20px;
  margin-bottom: 30px;
}

.panel {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 15px;
}

.ticket-list-container {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
}
</style>